package com.songlin.testv2ex.model;

import android.os.Parcel;
import android.os.Parcelable;


import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 松林 on 2015/10/4.
 */
public class Member extends SuperModel implements Parcelable {

    public int id;
    public String username;
    public String tagline;  //个性标语
    public String avatar;   //头像的URL
    public String website;  //个人网站
    public String github;   //github帐号
    public String twitter;
    public String location;

    public Member() {
    }

    private Member(Parcel in) {
        id = in.readInt();
        String[] strings = new String[7];
        in.readStringArray(strings);
        username = strings[0];
        tagline = strings[1];
        avatar = strings[2];
        website = strings[3];
        github = strings[4];
        twitter = strings[5];
        location = strings[6];
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeInt(id);
        parcel.writeStringArray(new String[] {
                username,   tagline,    avatar,  website,   github, twitter,    location
        });
    }

    public void parse(JSONObject jsonObject) throws JSONException {
        id = jsonObject.optInt("id");
        username = jsonObject.optString("username");
        tagline = jsonObject.optString("tagline");
        website = jsonObject.optString("website");
        github = jsonObject.optString("github");
        twitter = jsonObject.optString("twitter");
        location = jsonObject.optString("location");
        if ( !website.isEmpty() && !website.startsWith("http://") && !website.startsWith("https://")) {
            website = "http://" + website;
        }
        avatar = jsonObject.optString("avatar_large");
        if (avatar.startsWith("//"))
            avatar = "http:" + avatar;
    }

    public static final Creator<Member> CREATOR = new Creator<Member>() {
        @Override
        public Member createFromParcel(Parcel source) {
            return new Member(source);
        }
        @Override
        public Member[] newArray(int size) {
            return new Member[size];
        }
    };
}
